package zhoug.zui.autocomplete;

import android.content.Context;
import android.graphics.Rect;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;

/**
 * 自动补全输入框
 * @Author: zhoug
 * @Date: 2023-11-01
 * @Description:
 */
public class ZUIAutoCompleteTextView extends AppCompatAutoCompleteTextView {
    /**
     * 获得焦点并且没有任何输入时是否显示默认数据窗口
     */
    protected boolean hasFocusShowDefaultData = true;
    /**
     * 单击列表项监听
     */
    protected OnItemClickListener mOnItemClickListener;

    public ZUIAutoCompleteTextView(@NonNull Context context) {
        super(context);
        init();
    }

    public ZUIAutoCompleteTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ZUIAutoCompleteTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    protected void init() {
        setThreshold(1);
        super.setOnItemClickListener(new AdapterViewOnItemClickListener());
    }

    private class AdapterViewOnItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ListAdapter adapter = getAdapter();
            Object select=null;
            if (adapter != null) {
                select = adapter.getItem(position);
            }
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(parent, view, position, id, select);
            }
        }
    }

    @Override
    public boolean enoughToFilter() {
        if (hasFocusShowDefaultData) {
            super.enoughToFilter();
            return true;
        } else {
            return super.enoughToFilter();
        }
    }
    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (hasFocusShowDefaultData && focused) {
            Editable text = getText();
            if ( text != null && text.toString().length() == 0) {
                ListAdapter adapter = getAdapter();
                if (adapter instanceof Filterable) {
                    Filter filter = ((Filterable) adapter).getFilter();
                    filter.filter("");
                }
            }
        }
    }

    /**
     * 获得焦点并且没有任何输入时是否显示默认数据窗口
     * @param hasFocusShowDefaultData
     */
    public void setHasFocusShowDefaultData(boolean hasFocusShowDefaultData) {
        this.hasFocusShowDefaultData = hasFocusShowDefaultData;
    }

    /**
     * @see #setOnItemClickListener(OnItemClickListener)
     * @param l the item click listener
     */
    @Deprecated
    @Override
    public void setOnItemClickListener(AdapterView.OnItemClickListener l) {
        super.setOnItemClickListener(l);
    }

    /**
     * 单击数据列表监听
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }




}
